import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:provider/provider.dart';

class WifiInfo with ChangeNotifier {
  String _wifiName = '';

  String get wifiName => _wifiName;

  set wifiName(String value) {
    _wifiName = value;
    notifyListeners();
  }
}

main() {
  runApp(MultiProvider(
    providers: [ChangeNotifierProvider(create: (_) => WifiInfo())],
    child: MaterialApp(
      home: HomePage(),
    ),
  ));
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _startScan();
    });
    super.initState();
  }

  _startScan() {
    FlutterBluePlus.instance.scan(allowDuplicates: true).listen((event) {
      debugPrint('listen wifi device: ${event.device.toString()}');
      context.read<WifiInfo>().wifiName = event.device.name;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('蓝牙'),
      ),
      body: Selector<WifiInfo, String>(
        selector: (ctx, wifiInfo) {
          return wifiInfo.wifiName;
        },
        builder: (ctx, value, child) {
          return Text(
            value,
            style: TextStyle(fontSize: 26),
          );
        },
      ),
      // body: Column(
      //   children: [
      //     StreamBuilder<BluetoothState>(
      //       initialData: BluetoothState.unknown,
      //       stream: FlutterBluePlus.instance.state,
      //       builder: (ctx, snapshot) {
      //         final state = snapshot.data;
      //         debugPrint('state=$state');
      //         if (state == BluetoothState.on) {
      //           return _scaleDataWidget();
      //         }
      //         return _turnOnWidget();
      //       },
      //     ),
      //   ],
      // ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          FlutterBluePlus.instance.stopScan();
          // Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
          //   return Page2();
          // }));
        },
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _scaleDataWidget() {
    return StreamBuilder<ScanResult>(
        stream: FlutterBluePlus.instance.scan(allowDuplicates: true),
        builder: (ctx, snapshot) {
          debugPrint('scan result=${snapshot.data.toString()}');
          return Text(
            snapshot.data.toString(),
            style: TextStyle(fontSize: 26),
          );
        });
  }

  Widget _turnOnWidget() {
    FlutterBluePlus.instance.turnOn();
    return SizedBox();
  }
}

class Page2 extends StatelessWidget {
  const Page2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('page2'),
      ),
    );
  }
}
